import { BadRequestException, Injectable, PipeTransform } from '@nestjs/common';

@Injectable()
// 定义自定义管道，没穿参数给默认值
export class ParseOptionalIntPipe implements PipeTransform<string, number> {
  constructor(private defaultValue: number) {}

  transform(value: string): number {
    if (!value) {
      return this.defaultValue;
    }
    const result = parseInt(value);
    if (isNaN(result)) {
      throw new BadRequestException(
        `validation failed ${value} is not a number`,
      );
    }
    return result;
  }
}
